#ifndef THREAD
#define THREAD
#include <func.h>
#include "taskQueue.h"

#define NETDISK_LOG_ERROR(retval,val,msg){\
    time_t nowtime;\
    struct tm *timeInfo;\
    time(&nowtime);\
    timeInfo=localtime(&nowtime);\
    char timestamp[20];\
    strftime(timestamp,sizeof(timestamp),"%Y-%m-%d %H:%M:%S",timeInfo);\
    openlog("TEST", LOG_PID | LOG_CONS, LOG_USER);\
    if(retval==val){\
        syslog(LOG_ERR,"%s Error:%s,%s,%s,%d",timestamp,msg,__FILE__,__FUNCTION__,__LINE__);\
    }\
    else{\
    syslog(LOG_INFO,"%s user:%s",timestamp,msg);\
    }\
    closelog();\
}



typedef struct threadPool_s{
    int workNum;
    pthread_t* workArr;
    taskQueue_t task;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
}threadPool_t;

typedef struct sharInfo_s{
    int parentID;
    int rootID;
    int lastID;
    char user[50];
}sharInfo_t;

enum{
    OD_CD,
    OD_LS,
    OD_PTS,
    OD_GTC,
    OD_RM,
    OD_PWD,
    OD_MD,
    OD_RD,
    OD_REG,
    OD_LOG
};


typedef struct train_s{
    int status;
    int length;
    char buf[4096];
}train_t;


int initThreadPool(threadPool_t *thread,int num);
int makeTaskQueue(taskQueue_t * queue);
int enQueue(taskQueue_t * queue,int netfd);
int outQueue(taskQueue_t * queue);
int setTCP(char *IP,char*port,int*psockfd);
int epollAdd(int epfd,int fd);
int epollDel(int epfd,int fd);
int makeWork(threadPool_t * thread);
void* threadFunc(void* arg);
int changeDir(int netfd,sharInfo_t *sharInfo,MYSQL*mysql);
int get_pwd(sharInfo_t * sharInfo,MYSQL * mysql,char *path);
int get_grandpaID(sharInfo_t * sharInfo,MYSQL * mysql);
int checkDir(sharInfo_t*sharInfo,MYSQL*mysql,char* word,int *goalID);
int makeConnect(MYSQL *mysql);
int rm(int netfd,sharInfo_t *sharInfo,MYSQL * mysql);
int login(int netfd,sharInfo_t *sharInfo,MYSQL*mysql);
int pwd(int netfd,sharInfo_t * sharInfo,MYSQL *mysql);
int mysqlls(int netfd,sharInfo_t *sharInfo,MYSQL * mysql);
int makedir(int netfd,sharInfo_t *sharInfo,MYSQL * mysql);
int registerAcc(int netfd,MYSQL* mysql);
int removedir(int netfd,sharInfo_t * sharInfo,MYSQL *mysql);
int sersend(const int netfd,MYSQL* mysql,sharInfo_t* info);
int serrecv(const int netfd,MYSQL* mysql,sharInfo_t* info);
int remove_trailing_invisible_chars(char *str);
bool is_invisible_char(char c);
void generateRandomSalt(char *salt, int length);
int generateFormattedSalt(char *formattedSalt, int length);
#endif
